package com.neusoft.my12306.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.my12306.common.res.CommonResult;
import com.neusoft.my12306.entity.Schedule;
import com.neusoft.my12306.entity.Trainline;
import com.neusoft.my12306.mapper.ScheduleMapper;
import com.neusoft.my12306.mapper.TrainlineMapper;
import com.neusoft.my12306.service.ITrainlineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 段静迪
 * @since 2023-07-07
 */
@Service
public class TrainlineServiceImpl   implements ITrainlineService {
    @Autowired
    ScheduleMapper scheduleMapper;

    @Autowired
    TrainlineMapper  trainlineMapper;

    /**
     * 使用二次查询的方法完成数据库的一对多查询
     * why ？  连接查询
     * @return
     */
    @Override
    public CommonResult loadWithSchedules() {
        List<Trainline> lts =  trainlineMapper.selectList(null);

        for (int i = 0; i <lts.size() ; i++) {
            String  trainno   =  lts.get(i).getTrainno();

//            List<Schedule> lss =  scheduleMapper.loadByTrainno( trainno );
            List<Schedule> lss =  scheduleMapper.selectList( new QueryWrapper<Schedule>().eq("trainno", trainno) );
            lts.get(i).setSchedules(lss);

        }

        CommonResult result =new CommonResult( lts  );




        return result;
    }
}
